package com.chenc.unitdemo.jwt.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.crypto.digest.BCrypt;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenc.unitdemo.jwt.dao.UserDao;
import com.chenc.unitdemo.jwt.dto.UserDto;
import com.chenc.unitdemo.jwt.dto.UserInDto;
import com.chenc.unitdemo.jwt.dto.UserLoginDto;
import com.chenc.unitdemo.jwt.entity.User;
import com.chenc.unitdemo.jwt.golbal.BusinessException;
import com.chenc.unitdemo.jwt.service.IUserService;
import com.chenc.unitdemo.jwt.service.JWTService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static cn.hutool.crypto.digest.BCrypt.gensalt;

/**
*
* @description 用户表 服务实现类
* @author  chenc
* @since 2024-01-03 17:35:11
*/
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {

    @Autowired
    UserDao userDao;


    @Override
    public void saveDto(UserDto userDto) {
        User convert = Convert.convert(User.class, userDto);
        //密码加密
        String pwd = userDto.getPassword();
        String hashpw = BCrypt.hashpw(pwd,gensalt());
        convert.setPwd(hashpw);
//        log.info("保存用户"+JSONUtil.toJsonStr(convert));
//        boolean checkpw = BCrypt.checkpw(pwd, hashpw);

//        log.info("Check1,oripassword,{},sign,{},result,{} ",pwd,hashpw,checkpw);

        userDao.insert(convert);
    }

    @Override
    public void checkuser(UserDto userDto) {
        String username = userDto.getUsername();
        String password = userDto.getPassword();
        String nickname = userDto.getNickname();
        if (StringUtils.isEmpty(username)){
            throw new BusinessException("用户名必输");
        }
        if (StringUtils.isEmpty(password)){
            throw new BusinessException("密码必输");
        }
        if (StringUtils.isEmpty(nickname)){
            throw new BusinessException("别名必输");
        }
        Long userNameCount = userDao.selectCount(new LambdaQueryWrapper<User>().eq(User::getUsername, userDto.getUsername()));
        if (userNameCount.compareTo(0L)>0){
            throw new BusinessException("用户名称不可重复");
        }
        Long nickNameCount = userDao.selectCount(new LambdaQueryWrapper<User>().eq(User::getUsername, userDto.getNickname()));
        if (nickNameCount.compareTo(0L)>0){
            throw new BusinessException("用户别名不可重复");
        }

    }

    public static void main(String[] args) {
//        String pwd = "123456";
//        String sign = BCrypt.hashpw(pwd, gensalt());
//        System.out.println(sign);
////        String sign = "$2a$10$0tM0BX2cILmNjjwctQ2p2.VO6LmCH5gPAcnuTkhmYC.Da2Vf1CKsK";
//        boolean checkpw = BCrypt.checkpw(pwd, sign);
//        System.out.println(checkpw);
        System.out.println(BCrypt.checkpw("123456", "$2a$10$k7I0RycYvZwb47MbVBFPpe/PxYmek.tuVyCofloDQrjAQydwgRku6"));
        System.out.println(BCrypt.checkpw("12345678", "$2a$10$0tM0BX2cILmNjjwctQ2p2.VO6LmCH5gPAcnuTkhmYC.Da2Vf1CKsK"));
        System.out.println(BCrypt.checkpw("12345678", "$2a$10$IX6zznW6j76I2ktCDuzWku8lsn2f0RprpVeaUtiEgZmX0BOPXoOLy"));

    }

    @Override
    public void checkPwd(UserLoginDto userLoginDto) {

        User user = userDao.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, userLoginDto.getUsername()));
        if (null == user){
            throw new BusinessException("该账号未注册 请先注册");
        }
        boolean checkpw = BCrypt.checkpw(userLoginDto.getPassword(), user.getPwd());
        if (!checkpw){
            throw new BusinessException("密码验证错误");
        }
        userLoginDto.setUserId(String.valueOf(user.getUserId()));
        userLoginDto.setNickname(user.getNickname());
    }

    @Override
    public List<UserInDto> queryAllUserInfo() {
        List<User> users = userDao.selectList(new LambdaQueryWrapper<User>());
        if (CollectionUtils.isEmpty(users)){
            return new ArrayList<UserInDto>();
        }
        return Convert.toList(UserInDto.class,users);
    }

    @Autowired
    JWTService jwtService;
    @Override
    public void convertToken(UserLoginDto userLoginDto) {
        UserLoginDto token = jwtService.createToken(userLoginDto);
        jwtService.createTokenAndSendRedis(token);
    }


}
